﻿using System;
using System.Linq;
using System.Web.Security;
using SoftSource.Timecard.Web.Data;
using SoftSource.Timecard.Web.Resources;

namespace SoftSource.Timecard.Web.DAL
{
    public class Reports
    {
        public static object GetCostOfServicesData(DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportCostofServices(startDate, endDate).ToList();
            }
        }

        public static object GetCustomerSummaryData(int customerID)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportCustomerSummary(customerID).ToList();
            }
        }

        public static object GetEmployeeTravelData(DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportEmployeeTravel(startDate, endDate).ToList();
            }
        }

        public static object GetInvoiceByPersonData(DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportInvoiceByPerson(endDate).ToList();
            }
        }

        public static object GetInvoiceByPersonForProjectData(string projectName, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportInvoiceByPersonForProject(projectName, endDate).ToList();
            }
        }

        public static object GetInvoiceByPersonForProjectWithAnyBillStatusData(string projectName, DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportInvoiceByPersonForProjectWithAnyBillStatus(projectName, startDate, endDate).ToList();
            }
        }

        public static object GetLastEntryData()
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportLastEntry().ToList();
            }
        }

        public static object GetProjectsWithInvalidManagersData()
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportProjectsWithInvalidManagers().ToList();
            }
        }

        public static object GetEntriesPastDueData()
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportEntriesPastDue().ToList();
            }
        }

        public static object GetOvertimeData(DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportOvertime(startDate, endDate).ToList();
            }
        }

        public static object GetPTODetailData(DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportPTODetail(startDate, endDate).ToList();
            }
        }

        public static object GetPTOSummaryData(DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportPTOSummary(startDate, endDate).ToList();
            }
        }

        public static object GetPTOUserData(string username, DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportPTOUser(username, startDate, endDate).ToList();
            }
        }

        public static object GetUnapprovedEntriesData()
        {
            if (Roles.IsUserInRole(AuthenticationDataResources.ProjectManagerRole))
            {
                AuthenticationService authentication = new AuthenticationService();
                var user = authentication.GetUser();

                using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
                {
                    return db.ReportPMUnapprovedEntries(user.UserID).ToList();
                }
            }
            else
            {
                using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
                {
                    return db.ReportUnapprovedEntries().ToList();
                }
            }
        }

        public static object GetUtilizationData(DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportUtilization(startDate, endDate).ToList().Select(result =>
                {
                    if (result.IsAdministrative)
                        result.ExpectedHours = 0;
                    return result;
                });
            }
        }

        public static object GetWeeklySummaryData(DateTime startDate)
        {
            if (Roles.IsUserInRole(AuthenticationDataResources.ProjectManagerRole))
            {
                AuthenticationService authentication = new AuthenticationService();
                var user = authentication.GetUser();

                using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
                {
                    return db.ReportPMWeeklySummary(startDate, user.UserID).ToList();
                }
            }
            else
            {
                using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
                {
                    return db.ReportWeeklySummary(startDate).ToList();
                }
            }
        }

        public static object GetWRQSummaryData()
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportWRQSummary().ToList();
            }
        }

        internal static object GetProjectSummaryData(int projectID, DateTime startDate, DateTime endDate)
        {
            using (StaticDBDataContext db = Utilities.GenerateStaticDataContext())
            {
                return db.ReportProjectSummary(projectID, startDate, endDate).ToList();
            }
        }
    }
}
